// pages/receive/addContract/addContract.js
import api from '../../../utils/api';
import util from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: [],
    searchValue: '',
    currentPage: 1, // 当前页码
    pageSize: 10, // 每页显示数据
    loading: true,
    remind: '下拉加载更多',
    nodata: false, //是否没有数据
    contractData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    console.log(options)
    this.data.claimId = options.claimId;
    this.data.customerId = options.customerId&&options.customerId!='null'?options.customerId:'';
    this.getData()
  },
  getData() {
    let that = this;
    console.log( that.data.customerId)
    api.claimSelectContract({
      data: {
        companyId: wx.getStorageSync('userInfo').coId,
        claimId: that.data.claimId,
        customerId: that.data.customerId,
        searchStr: that.data.searchValue ? that.data.searchValue : '',
        pageNum: that.data.currentPage,
        pageSize: that.data.pageSize
      },
      success: function (res) {
        if (res.data.code == 200) {
          if(res.data.data.list.length>0){
            for (let i = 0; i < res.data.data.list.length; i++) {
              let now = new Date(res.data.data.list[i].signDate);
              res.data.data.list[i].signDate = res.data.data.list[i].signDate?util.formatTime(now).split(' ')[0]:'';
              res.data.data.list[i].contractMoney = util.formatFigure(res.data.data.list[i].contractMoney)
            }
            that.data.totalPage = res.data.data.totalPage;
            that.setData({
              contractData: that.data.contractData.concat(res.data.data.list),
              loading: false,
              nodata: false,
              remind: res.data.data.totalPage > res.data.data.pageNum ? '下拉加载更多' : '已经到底啦～'
            })
          }else{
            that.setData({
              nodata:true
            })
          }
          
        }
      }
    })
  },
  // 搜索
  onSearch(event) {
    this.setData({
      searchValue: event.detail && event.detail.value ? event.detail.value : event.detail,
    })
    this.clearData()
  },
  clickadd_hetong(){
    wx.navigateTo({
      url: '/pages/addNewbill/addNewbill',
    })
  },
  // 清空数据并重新加载数据
  clearData() {
    this.setData({
      contractData: [],
      currentPage: 1,
      loading:true,
      tips: '暂无数据'
    })
    this.getData()
  },
  onChange(event) {
    this.setData({
      result: event.detail,
    });
  },
  onConfirm(){
    console.log(this.data.result)
    console.log(this.data.result.join(','))
    let that = this;
    if(this.data.result.length>0){
      api.claimRelateContract({
        data:{
          claimId:that.data.claimId,
          contractStr:that.data.result.join(',')
        },
        success:function(res){
          if(res.data.code==200){
            wx.showToast({
              title: '添加成功',
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1,
              })
            }, 100);
          }else{
            wx.showModal({
              content:res.data.message,
              showCancel:false,
              confirmColor:'#0041C4'
            })
          }
        }
      })
    }else{
      wx.showModal({
        content:'请选择要添加的发票',
        showCancel:false,
        confirmColor:'#0041C4'
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.clearData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.totalPage > this.data.currentPage) {
      ++this.data.currentPage;
      this.setData({
        loading:true
      })
      this.getData()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})